#include "Sprite.h"

void Sprite::Initialise(HDC *bHDC, LPSTR filename, int xpos, int ypos, float sWidth, float sHeight, float mass, float e, bool hit)
{
	backHDC = bHDC;
	bitmapHDC = CreateCompatibleDC(*backHDC);
	x = xpos;	// starting position
	y = ypos;	// starting position
	scaleWidth = sWidth;
	scaleHeight = sHeight;

	RigidBody.Initialise(x, y, width, height, mass, e, hit);
	LoadABitmap(filename);	// load image
	if(!bitmap)
	{
		//failed to load image
		MessageBox(NULL, "Image fail to load", "Error", MB_OK);
	}

}

void Sprite::LoadABitmap(LPSTR szFileName)
{
	bitmap = (HBITMAP)LoadImage(NULL, szFileName, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);

	BITMAP temp;
	GetObject (bitmap, sizeof(temp), &temp);
	width = temp.bmWidth;
	height = temp.bmHeight;
}

//void Sprite::drawSprite()
//{
//	HBITMAP originalBitMap;
//
//	//remember to add the bitmapHDC to scene.h
//	originalBitMap = (HBITMAP)SelectObject(bitmapHDC, bitmap);
//	
//	// blt the image to the back buffer
//	BitBlt(*backHDC, x, y, width, height, bitmapHDC, 0, 0, SRCCOPY);
//
//	SelectObject(bitmapHDC, originalBitMap); 
//}

void Sprite::drawSprite(int r, int g, int b)
{
	HBITMAP originalBitMap;

	//remember to add the bitmapHDC to scene.h
	originalBitMap = (HBITMAP)SelectObject(bitmapHDC, bitmap);
	
	//Transparent blt the image to the back buffer using the value colours passed in at drawSprite call
	TransparentBlt(*backHDC, x, y, width, height, bitmapHDC, 0, 0, width, height, RGB (r, g, b));

	SelectObject(bitmapHDC, originalBitMap); 
}

void Sprite::drawSprite()
{
	HBITMAP originalBitMap;

	//remember to add the bitmapHDC to scene.h
	originalBitMap = (HBITMAP)SelectObject(bitmapHDC, bitmap);
	
	//Stretch blt the image to the back buffer using valuse passed in at call
	StretchBlt(*backHDC, x, y, width*scaleWidth, height*scaleHeight, bitmapHDC, 0, 0, width, height, SRCCOPY);

	SelectObject(bitmapHDC, originalBitMap); 
}

void Sprite::drawSprite(int xpos0, int ypos0, int xpos1, int ypos1, int xpos2, int ypos2)
{
	POINT pt[3];
	//rotates image
	pt[0].x = xpos0;
	pt[0].y = ypos0;
	pt[1].x = xpos1;
	pt[1].y = ypos1;
	pt[2].x = xpos2;
	pt[2].y = ypos2;

	HBITMAP originalBitMap;

	//remember to add the bitmapHDC to scene.h
	originalBitMap = (HBITMAP)SelectObject(bitmapHDC, bitmap);
	
	//Palarelogram blt the image to the back buffer using values in a point array passed in at call
	PlgBlt(*backHDC, pt, bitmapHDC, 0, 0, width, height, NULL, 0, 0);

	SelectObject(bitmapHDC, originalBitMap); 
}